package com.disapand.strongarmy.controller

import com.disapand.strongarmy.service.*
import com.disapand.strongarmy.vo.*
import org.springframework.format.annotation.*
import org.springframework.web.bind.annotation.*
import java.time.*

/**
 *
 * @auth disapand
 * @date 2021/10/19 - 21:52 - 星期二
 *
 */
@RestController
@RequestMapping("/roles", name = "角色管理")
class RoleController(private val roleService: RoleService) {

    /**
     * 添加角色
     *
     * @param label
     * @param role
     * @return
     */
    @PostMapping("/add", name = "添加角色")
    fun add(
        @RequestParam("label") label: String,
        @RequestParam("role") role: String
    ): ResponseVO {
        val result = roleService.add(label, role)
        return if (result) {
            ResponseVO.ofSuccess()
        } else {
            ResponseVO.ofError()
        }
    }

    /**
     * 分页获取角色列表
     *
     * @param label
     * @param role
     * @param status
     * @param createdBy
     * @param createdAt
     * @param createdStartAt
     * @param createdEndAt
     * @param updatedBy
     * @param updatedAt
     * @param updatedStartAt
     * @param updatedEndAt
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/page", name = "分页获取角色列表")
    fun page(
        @RequestParam("label", required = false) label: String?,
        @RequestParam("role", required = false) role: String?,
        @RequestParam("status", required = false) status: List<Int>?,
        @RequestParam("created_by", required = false) createdBy: Int?,
        @RequestParam("created_at", required = false) createdAt: String?,
        @RequestParam(
            "created_start_at",
            required = false
        ) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") createdStartAt: LocalDateTime?,
        @RequestParam(
            "created_end_at",
            required = false
        ) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") createdEndAt: LocalDateTime?,
        @RequestParam("updated_by", required = false) updatedBy: Int?,
        @RequestParam("updated_at", required = false) updatedAt: String?,
        @RequestParam(
            "updated_start_at",
            required = false
        ) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") updatedStartAt: LocalDateTime?,
        @RequestParam(
            "updated_end_at",
            required = false
        ) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") updatedEndAt: LocalDateTime?,
        @RequestParam("current", required = false, defaultValue = "1") current: Int,
        @RequestParam("page_size", required = false, defaultValue = "20") pageSize: Int,
    ): ResponseVO {
        val page = roleService.page(
            label,
            role,
            status,
            createdBy,
            createdAt,
            createdStartAt,
            createdEndAt,
            updatedBy,
            updatedAt,
            updatedStartAt,
            updatedEndAt,
            current,
            pageSize
        )
        return ResponseVO.ofSuccess(page)
    }

    /**
     * 改变角色状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status", name = "改变角色状态")
    fun flushStatus(
        @RequestParam("id") id: List<Int>,
        @RequestParam("status", defaultValue = "0", required = false) status: Int,
    ): ResponseVO {
        return ResponseVO.ofSuccess(roleService.flushStatus(id, status))
    }

    /**
     * 编辑角色信息
     *
     * @param id
     * @param label
     * @param role
     * @return
     */
    @PutMapping("/edit", name = "编辑角色")
    fun edit(
        @RequestParam("id") id: Int,
        @RequestParam("label") label: String,
        @RequestParam("role") role: String,
    ): ResponseVO {
        return ResponseVO.ofSuccess(roleService.edit(id, label, role))
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del", name = "删除角色")
    fun delete(
        @RequestParam("id") id: List<Int>
    ): ResponseVO {
        return ResponseVO.ofSuccess(roleService.delete(id))
    }
}